package com.niit.project.controller;

import com.niit.project.entity.Admin;
import com.niit.project.service.AdminService;
import com.niit.project.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;

//张笑玉——管理员登陆
@WebServlet(name = "AdminLoginController", value = "/AdminLoginController")
public class AdminLoginController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收前端的用户输入的数据
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        AdminService adminService = new AdminServiceImpl();
        Admin admin = adminService.login(name, password);
        if (admin != null) {
            System.out.println("登陆成功");
            //获取session对象,浏览器不关闭会话对象的信息就不会消失
            HttpSession session = request.getSession();
            //向session对象中存储对象信息
            session.setAttribute("admin",admin);
            //重定向到展示所有商品的servlet中
            response.sendRedirect(request.getContextPath()+"/ShowAllGoodsController");

        } else {
            System.out.println("登陆失败");
        }
    }
}
